package com.cat.dynamicPlanning;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/closest-dessert-cost/
 * @create 2025/9/2 13:53
 * @since JDK17
 */

public class Solution48 {
    int ans = 0x3f3f3f3f;
    void f(int cur, int sum, int[] t, int tar) {
        if (cur == t.length) {
            return ;
        }
        int a = Math.abs(sum - tar), b = Math.abs(ans - tar);
        if (a < b || (a == b && sum < ans)) {
            ans = sum;
        }
        f(cur + 1, sum, t, tar);
        f(cur + 1, sum + t[cur], t, tar);
        f(cur + 1, sum + t[cur] * 2, t, tar);
    }


    public int closestCost(int[] b, int[] t, int target) {
        for (var x : b) {
            f(0, x, t, target);
        }
        return ans;
    }
}
